'''
Created on May 9, 2012

@author: dbbaskette
'''

#from NodeCheck import NodeCheck
from Node import Node
from User import User
from Disk import Disk
from Filesystem import Filesystem
from PreCheck import PreCheck
from NodeCheck import NodeCheck
from SystemConfiguration import SystemConfiguration
import csv
from InstallStatus import InstallStatus
from StorageOps import StorageOps
from Software import Software
from XML_File import XML_File
from NodeInfo import NodeInfo
from Java import Java
import logging
import os
import datetime


def StartLogs():
        
        '''
        Currently this logs every log type.  Eventually this will default
        to just logging errors and information unless the --debug flag is used.
        '''
        
        '''
        THIS IS ALSO IN PACYDERM,SO IT WILL WORK WHEN APP IS DEPLOYED'''
        
        if os.path.isfile("./log/pachyderm.log"):
            os.system("mv ./log/pachyderm.log ./log/"+datetime.datetime.now().strftime("%Y%m%d-%H:%M")+"-pachyderm.log")
        logging.basicConfig(filename='./log/pachyderm.log',level=logging.INFO,format='%(asctime)s %(levelname)s:%(message)s')
        logging.info("Pachyderm Logging started...")


if __name__ == '__main__':
    
    Nodes = []
    Users = []
    
    StartLogs()
   
    
#    for i in range (1,5,1):
#        nodeName = "hadoop0"+str(i)
#        node = Node(nodeName,"10.10.88.245","*njd")
#        disk1 = Disk("/dev/sdb",False,"xfs",0,0)
#        disk2 = Disk("/dev/sdc",False,"xfs",0,0)
#        disk3 = Disk("/dev/sdd",False,"xfs",0,0)
#        disk4 = Disk("/dev/sde",False,"xfs",0,0)
#        disk5 = Disk("/dev/sdf",False,"xfs",0,0)
#
#        node.AddDisk(disk1)
#        node.AddDisk(disk2)
#        node.AddDisk(disk3)
#        node.AddDisk(disk4)
#        node.AddDisk(disk5)
#
#        node.ipAddress = socket.gethostbyname(nodeName)        
#
#        Nodes.append(node)
    pc = PreCheck(Nodes,Users)
    user = User("root","password","i","i",False)

    Users.append(user)
#    user = User("hdfs","pass6word","h","h",False)
#    Users.append(user)
#    user = User("mapred","pas6word","m","m",False)
#    Users.append(user)
    
    
    
    
    
 
   

#    
    for row in csv.DictReader(open("/tmp/cluster.csv"), 'hostname ipaddress personalities fstype disks'.split()):
            
        hostname = row['hostname'] 
        ipAddress = row['ipaddress']     
        personalities = row['personalities']
        fstype = row['fstype']
        node = Node(row['hostname'],row['ipaddress'],row['personalities'])

        for disk in str(row['disks']).split(":"):
            node.AddDisk(Disk("/dev/"+str(disk),0,fstype,0,0))
        Nodes.append(node)

   # installStatus = InstallStatus()
    
    baseDir = "./conf/config_files"
    mountPoint = "/gphd_data"
    dataDir = "/disk"
    

    nc = NodeCheck(Nodes,Users)
   
    if (nc.ConnectivityCheck() < 0):
        print "Error"
        
    nc.Inventory()    
        
        
    java=Java(Nodes,Users)
    java.CheckJDK()
    #java.PushJDK("/root/Downloads/jdk-7u5-linux-x64.rpm","/tmp/jdk-7u5-linux-x64.rpm")
    java.removeOpenJDK()
    
#    sysConfig = SystemConfiguration(Nodes,Users,baseDir)
#    
#    sysConfig.BuildClusterDefinitionFile("masters")
#    sysConfig.BuildClusterDefinitionFile("slaves")
#    
#
#    status = sysConfig.SSHKeyShare()
#
# 
#
#    storage = StorageOps(Nodes,Users)
#    rtn = storage.Partition()
##    if rtn == -1:
##        print "Error"
#
#
#
#    fs = Filesystem(Nodes,Users,mountPoint,dataDir)
#    status = fs.install()
#    fs.Mount()
#    fs.VerifyMounts()
#    fs.CreateDataDirectories("hadoop")
#
    binDir = "/root/downloads"
    binName = "gphd-1.1-rpm-dist.tar.gz"
#
#    software = Software(Nodes,Users,binDir,binName)
#    software.PrepareInstallFile()
#    HadoopPrj = software.BuildHadoopProject()
#    software.PushProject(HadoopPrj)
#    software.InstallProject(HadoopPrj)
#    #software.RemoveProject(HadoopPrj)
#    software.VerifyProject(HadoopPrj)
#
#    sysConfig.ModifyXMLConfig("core-site.xml")
#    sysConfig.ModifyXMLConfig("mapred-site.xml")
#    sysConfig.ModifyXMLConfig("hdfs-site.xml")
#    sysConfig.PushXMLConfig("core-site.xml") 
#    sysConfig.PushXMLConfig("mapred-site.xml")
#    sysConfig.PushXMLConfig("hdfs-site.xml")
#    
#    for node in Nodes:
#        for disk in node.Disks:
#            print str(node.hostName)+":"+str(disk.deviceName)+":"+str(disk.mountPath)
#    
    